package org.feng.service.impl;

import lombok.AllArgsConstructor;
import org.feng.service.EmailService;
import org.feng.utils.UUIDUtil;
import org.feng.vo.EmailMessageVO;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @Author feng
 * @Date 2022/6/7 10:11
 * @Description TODO
 */
@Service
@AllArgsConstructor
public class EmailServiceImpl implements EmailService {

    private JavaMailSender javaMailSender;
    private static final String FROM_EMAIL = "15612509687@163.com";

    @Override
    public Boolean sendEmail(EmailMessageVO message) {
        System.out.println("第二个");
        try {
            javaMailSender.send(getMimeMessage(message));
            return true;
        } catch (MailException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Boolean sendEmailWithAttach(EmailMessageVO message, MultipartFile[] file) {
        System.out.println("第二个");
        try {
            javaMailSender.send(getMimeMessage(message, file));
            return true;
        } catch (MailException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 获取发送消息主体
     *
     * @param messageVO
     * @return
     */
    public MimeMessage getMimeMessage(EmailMessageVO messageVO) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        getMessageHelper(mimeMessage, messageVO);
        return mimeMessage;
    }

    /**
     * 获取发送消息主体
     *
     * @param messageVO
     * @param file
     * @return
     */
    public MimeMessage getMimeMessage(EmailMessageVO messageVO, MultipartFile[] file) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper messageHelper = getMessageHelper(mimeMessage, messageVO);
            if (file != null && file.length > 0) {
                for (MultipartFile multipartFile : file) {
                    FileSystemResource fileSystemResource = new FileSystemResource(multipartFileToFile(multipartFile));
                    messageHelper.addAttachment(fileSystemResource.getFilename(), fileSystemResource);
                }
            }
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return mimeMessage;
    }

    public MimeMessageHelper getMessageHelper(MimeMessage mimeMessage, EmailMessageVO messageVO) {
        MimeMessageHelper messageHelper = null;
        try {
            messageHelper = new MimeMessageHelper(mimeMessage, true);
            messageHelper.setFrom(FROM_EMAIL);
            messageHelper.setTo(messageVO.getSendTargets());
            messageHelper.setSubject(messageVO.getTitle());
            messageHelper.setText(messageVO.getContent(), true);
            if (messageVO.getCc() != null) {
                messageHelper.setCc(messageVO.getCc());
            }
            if (messageVO.getBcc() != null) {
                messageHelper.setBcc(messageVO.getBcc());
            }
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return messageHelper;
    }

    /**
     * 将MultipartFile转换为File
     *
     * @param multiFile
     * @return
     */
    public static File multipartFileToFile(MultipartFile multiFile) {
        // 获取文件名
        String fileName = multiFile.getOriginalFilename();
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        // 若须要防止生成的临时文件重复,能够在文件名后添加随机码
        try {
            File file = File.createTempFile(UUIDUtil.getUUID(), prefix);
            multiFile.transferTo(file);
            return file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
